package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwbwT;
import cn.e4j.modular.crm.service.GwbwTService;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 行业一级分类管理
 * Created by miaoyl on 2019-1-9.
 */
@Controller
@RequestMapping("/crm/gwai236")
public class Gwai236Controller extends BaseController {

    @Autowired
    private GwbwTService gwbwTService;

    /**
     * 跳转行业一级分类管理页面
     */
    @RequiresPermissions("crm:gwai236:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwai236.html";
    }

    /**
     * 跳转到查看编辑的页面
     */
    @RequestMapping("/gwai236_add")
    public String addView() {
        return "/pc/crm/gwai236_edit.html";
    }

    /**
     * 查询行业一级分类信息列表
     */
    @RequestMapping("/getGwbwlist")
    @RequiresPermissions("crm:gwai236:init")
    @ResponseBody
    public ResponseData getGwbwlist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("ent",shiroUser.getDBC());
        inDto.put("lang",LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> result = gwbwTService.getGwbwlist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwai236:init")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        // 判断编号是否重复
        GwbwT theGwbwT = gwbwTService.getByGwbw001(shiroUser.getDBC(),inDto.getString("GWBW001"));
        if (theGwbwT != null) {
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_CODE.getCode() ,
                    CrmExceptionEnum.EXISTED_THE_CODE.getMessage());
            //throw new ServiceException(CrmExceptionEnum.EXISTED_THE_CODE);
        }
        gwbwTService.add(inDto);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwai236:init")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("ent",shiroUser.getDBC());
        inDto.put("GWBWMODU",shiroUser.getACCOUNT());
        gwbwTService.edit(inDto);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwai236:init")
    @ResponseBody
    public ResponseData del(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("ent",shiroUser.getDBC());
        gwbwTService.delete(inDto);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwai236:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbwT> gwbwTList = inDto.getReqList(GwbwT.class);
        gwbwTService.delDataBatch(gwbwTList);
        return SUCCESS_TIP;
    }
}
